package com.ymatou.iapi.quickturnover.testcase;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.quickturnover.parameter.GrantCreditReqBean;
import com.ymatou.iapi.quickturnover.service.AdvanceOrderService;
import com.ymatou.iapi.quickturnover.service.GrantCreditReqCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.AdvanceOrder;
import com.ymttest.database.sqlwapper.AdvanceOrderDefaultWapper;
import com.ymttest.utils.EnvSetup;
/**
*授信接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GrantCreditReq {
	private static GrantCreditReqBean grantcreditreqBean;
	private static GrantCreditReqCall grantcreditreqCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("授信接口");
	}

	@Before
	public void caseUp() {
		grantcreditreqBean = new GrantCreditReqBean();
		grantcreditreqCall = new GrantCreditReqCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(grantcreditreqCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GrantCreditReq_001() {
		Logger.start(true, "授信成功");
		try {
			Map map = AdvanceOrderService.createAdvanceOrder();

			int orderId = (Integer) map.get("orderId");
			int sellerId = (Integer) map.get("sellerId");
			int userId = (Integer) map.get("userId");

			Thread.sleep(100000);

			grantcreditreqBean.setSellerId(sellerId);
			grantcreditreqCall.setData(grantcreditreqBean);
			grantcreditreqCall.callService();
			Logger.verifyEquals("ok", grantcreditreqCall.getReturnData(),
					"验证接口返回ok");

			Thread.sleep(3000);

			AdvanceOrderDefaultWapper advanceOrderDefaultWapper = new AdvanceOrderDefaultWapper();
			//advance_order
			Map advance = new HashMap();
			advance.put("order_id", orderId);
			List<AdvanceOrder> advanceOrderList = advanceOrderDefaultWapper
					.selectAdvanceOrder(advance);
			Logger.comment("校验advance_order表");
			advanceOrderList.forEach(advanceOrder -> {
				BigDecimal order_amount = advanceOrder.getOrder_amount();
				BigDecimal rate = advanceOrder.getAdvance_rate();
				Logger.verifyEquals(
						advanceOrder.getCan_advance_amount(),
						order_amount.multiply(rate).compareTo(
								new BigDecimal(1000)) > 1
								? new BigDecimal(1000).setScale(2)
								: order_amount.multiply(rate).setScale(2),
						"验证can_advance_amount");
				Logger.verifyEquals(2, advanceOrder.getAdvance_status(),
						"验证advance_status");
				Logger.verifyEquals(sellerId, advanceOrder.getSeller_id(),
						"验证seller_id");
				Logger.verifyEquals(userId, advanceOrder.getUser_id(),
						"验证user_id");
				Logger.verifyEquals(orderId, advanceOrder.getOrder_id(),
						"验证order_id");
			}

			);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	//手工验证，创建大于1000元订单
	public void Tc_GrantCreditReq_002() {
		Logger.start(true, "订单最大可预支金额大于限定的最高支取额度");
		try {
			Map map = AdvanceOrderService.createAdvanceOrder();
			long orderId = (Long) map.get("orderId");
			int sellerId = (Integer) map.get("sellerId");
			int userId = (Integer) map.get("userId");

			grantcreditreqBean.setSellerId(sellerId);
			grantcreditreqCall.setData(grantcreditreqBean);
			grantcreditreqCall.callService();

			Thread.sleep(2000);

			AdvanceOrderDefaultWapper advanceOrderDefaultWapper = new AdvanceOrderDefaultWapper();
			//advance_order
			Map advance = new HashMap();
			advance.put("order_id", orderId);
			List<AdvanceOrder> advanceOrderList = advanceOrderDefaultWapper
					.selectAdvanceOrder(advance);
			Logger.comment("校验advance_order表");
			advanceOrderList.forEach(advanceOrder -> {
				BigDecimal order_amount = advanceOrder.getOrder_amount();
				BigDecimal rate = advanceOrder.getAdvance_rate();

				Logger.verifyEquals(advanceOrder.getCan_advance_amount(),
						order_amount.multiply(rate).setScale(2),
						"验证can_advance_amount");
				Logger.verifyEquals(2, advanceOrder.getAdvance_status(),
						"验证advance_status");
				Logger.verifyEquals(sellerId, advanceOrder.getSeller_id(),
						"验证seller_id");
				Logger.verifyEquals(userId, advanceOrder.getUser_id(),
						"验证user_id");
				Logger.verifyEquals(orderId, advanceOrder.getOrder_id(),
						"验证order_id");
			}

			);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GrantCreditReq_003() {
		Logger.start(true, "sellerId 不存在");
		try {
			int sellerId = 11;
			grantcreditreqBean.setSellerId(sellerId);
			grantcreditreqCall.setData(grantcreditreqBean);
			grantcreditreqCall.callService();
			Logger.verifyEquals("ok", grantcreditreqCall.getReturnData(),
					"验证接口返回ok");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GrantCreditReq_004() {
		Logger.start(true, "sellerId 为null");
		try {
			//int sellerId = 11;		
			//grantcreditreqBean.setSellerId(sellerId);
			grantcreditreqCall.setData(grantcreditreqBean);
			grantcreditreqCall.callService();
			Logger.verifyEquals("ok", grantcreditreqCall.getReturnData(),
					"验证接口返回ok");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GrantCreditReq_005() {
		Logger.start(true, "授信");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			grantcreditreqBean.setSellerId(sellerId);
			grantcreditreqCall.setData(grantcreditreqBean);
			grantcreditreqCall.callService();
			Logger.verifyEquals("ok", grantcreditreqCall.getReturnData(),
					"验证接口返回ok");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}